package com.carkepper.appraisal.controller;

import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.utile.QiniuUtils;
import com.carkepper.appraisal.dao.AppraisalMongoDao;
import com.carkepper.appraisal.service.impl.AppraisalMongoServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 服务评价的mongo的controller层
 */
@RestController
@RequestMapping("/appraisal")
@Api(tags = "服务评价的api")//标签专门用来标注类的
@CrossOrigin //解决跨域
public class AppraisalMongoController {

    @Autowired
    private AppraisalMongoServiceImpl appraisalMongoServiceImpl;

    // 1.增加评论
    @PostMapping("/save")
    @ApiOperation("增加评论") //标注方法
    public Result<AppraisalMongoDao> saveObj(@RequestBody AppraisalMongoDao mongoDao) {
        return appraisalMongoServiceImpl.saveObj(mongoDao, fileFull);
    }

    // 2.查询全部
    @GetMapping("findAll")
    @ApiOperation("查询评论列表") //标注方法
    public Result<AppraisalMongoDao> findAll() {
        return appraisalMongoServiceImpl.findAll();
    }

    // 3.根据id查询
    @GetMapping("/findOne/{id}")
    @ApiOperation("根据id查询") //标注方法
    public Result<AppraisalMongoDao> findOne(@PathVariable("id") Integer id) {
        return appraisalMongoServiceImpl.getAppraiseById(id);
    }

    // 4.根据对象真实删除
    @PostMapping("/delOne")
    @ApiOperation("根据对象真实删除") //标注方法
    public Result<AppraisalMongoDao> delOne(@RequestBody AppraisalMongoDao mongoDao) {
        return appraisalMongoServiceImpl.deleteAppraisalMongoDao(mongoDao);
    }

    // 5.根据id真实删除
    @GetMapping("/delById/{id}")
    @ApiOperation("根据id真实删除") //标注方法
    public Result<AppraisalMongoDao> delById(@PathVariable("id") Integer id) {
        return appraisalMongoServiceImpl.deleteAppraiseBy(id);
    }

    // 6.分页查询
    @GetMapping("/fendPage/{pageIndex}/{pageSize}")
    @ApiOperation("分页查询") //标注方法
    public PageResult<AppraisalMongoDao> fendPage(@PathVariable("pageIndex") Integer pageIndex,
                                                  @PathVariable("pageSize") Integer pageSize) {
        // 3.返回
        return appraisalMongoServiceImpl.pageQuery(pageIndex, pageSize);
    }


    // 7.根据多条件查询
    @GetMapping("/getMany/{pageIndex}/{pageSize}")
    @ApiOperation("根据多条件查询") //标注方法
    public PageResult<AppraisalMongoDao> getMany(String orderId, String customorName,
                                                 String orderCreateTime, String storeSimpleName,
                                                 String serviceKind,
                                                 @PathVariable(name = "pageIndex", required = false) Integer pageIndex,
                                                 @PathVariable(name = "pageSize", required = false) Integer pageSize) {
        // 1.传递条件，调用service层方法，得到数据
        PageResult<AppraisalMongoDao> pageList = appraisalMongoServiceImpl.getAppraisalMongoDaoByQuery(orderId,
                customorName, orderCreateTime, storeSimpleName, serviceKind, pageIndex, pageSize);
        // 2.返回数据
        return pageList;
    }

    // 8.逻辑删除的方法
    @PutMapping("/delete/{id}")
    @ApiOperation("逻辑删除的方法") //标注方法
    public Result<AppraisalMongoDao> delete(@PathVariable("id") Integer id) {
        return appraisalMongoServiceImpl.update(id);
    }

    // 9.恢复逻辑删除
    @PutMapping("/deleteBack/{id}")
    @ApiOperation("恢复逻辑删除") //标注方法
    public Result<AppraisalMongoDao> deleteBack(@PathVariable("id") Integer id) {
        return appraisalMongoServiceImpl.updateBack(id);
    }

    // 10.文件上传
    /**
     *  文件上传的方法
     * @param imgFile 文件名
     * @return
     */
    private String fileFull = ""; //扩大图片回显的范围
    @PostMapping("upload")
    @ApiOperation("文件上传") //标注方法
    public String upload(MultipartFile imgFile) {
        try {
            // 1.获取文件名字
            String fileName = imgFile.getOriginalFilename();
            // 去掉例如.jpg的后缀
            int index = fileName.lastIndexOf(".");
            String extention = fileName.substring(index - 1);
            //获得图片回显的访问路径
            fileFull = UUID.randomUUID().toString() + extention;
            // 2.调用七牛云工具类，上传文件
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            // 3.上传成功，响应的信息
            return fileFull;
        } catch (IOException e) {
            e.printStackTrace();
            // 4.上传失败，响应的信息
            return null;
        }
    }

}
